package main

import (
	"database/sql"
	"github.com/kataras/iris"
	"log"
	"strings"
	 _ "github.com/go-sql-driver/mysql"
)
/** 
环境
path => /usr/lib/golang/src/github.com 
*/
func main() {
	app := iris.Default()

	app.Handle("GET","/",func(ctx iris.Context){
		ctx.HTML("Hi,Alex. Hello Golang!")
	})

	app.Get("/ping",func(ctx iris.Context){
		ctx.WriteString("pong")
	})

	app.Get("/hello",func(ctx iris.Context){
		ctx.JSON(iris.Map{"message":"Hello isis web framework."})
	})
	
	app.Get("/data",func(ctx iris.Context){
		log.Printf("this is mysql data opetion.")
		var ss []string
		db, err := sql.Open("mysql","dev_club_rwl:OesEPzp1@tcp(192.168.1.1:3306)/dbsys?charset=utf8")
		if err != nil{
			panic(err.Error())
			log.Println(err)
				return
		}
		rows, err := db.Query("SELECT 1")
		if err != nil{
			log.Fatalln(err)
		}

		for rows.Next(){
			var s string
			err = rows.Scan(&s)
			if err !=nil{
				log.Fatalln(err)
			}
			//log.Printf("found row containing %q", s)
			ss = append( ss, s )	
		}
		rows.Close()
		
		defer db.Close()
		ctx.WriteString(strings.Join(ss,","))
	})
	
	//在 Tcp 上监听网络地址 0.0.0.0:8080
	app.Run(iris.Addr(":8080"))
}